package tech.veedo.munchkin.verifications.backend.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import tech.veedo.munchkin.beans.backend.Beginner;
import tech.veedo.munchkin.constants.Operation;
import tech.veedo.munchkin.constants.Type;
import tech.veedo.munchkin.verifications.backend.BeginnerVerify;

import java.util.List;

@Slf4j
@Service
public class BeginnerVerifyImpl implements BeginnerVerify {
    @Override
    public boolean verify(Operation operation, Beginner beginner) {
        switch (operation) {
            case ADD:
                return addVerify(beginner);
            case UPDATE:
                return updateVerify(beginner);
            case QUERY:
                return queryVerify(beginner);
            default:
                return true;
        }
    }

    private boolean queryVerify(Beginner beginner) {
        workflowIDNotNull(beginner.getWorkflowID());
        orgCodeNotNull(beginner.getOrgCode());
        return true;
    }

    @Override
    public boolean verifyBatch(Operation operation, List<Beginner> beginners) {
        switch (operation) {
            case ADD:
                for (Beginner beginner : beginners) {
                    addVerify(beginner);
                }
            case UPDATE:
                for (Beginner beginner : beginners) {
                    updateVerify(beginner);
                }
            default:
                return true;
        }
    }

    private boolean addVerify(Beginner beginner) {
        typeNotNull(beginner.getType());
        if (beginner.getType() == Type.PERSON) {
            psnIDNotNull(beginner.getPsnID());
            psnNameNotNull(beginner.getPsnName());
        }
        postCodeNotNull(beginner.getPostCode());
        postNameNotNull(beginner.getPostName());
        workflowIDNotNull(beginner.getWorkflowID());
        orgCodeNotNull(beginner.getOrgCode());
        return true;
    }

    private boolean updateVerify(Beginner beginner) {
        idNotNull(beginner.getId());
        return true;
    }

    private void idNotNull(Long id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new NullPointerException("发起人员的id为空");
        }
    }

    private void orgCodeNotNull(String orgCode) {
        if (StringUtils.isEmpty(orgCode)) {
            throw new NullPointerException("发起人员的发起工作流机构标识为空");
        }
    }

    private void workflowIDNotNull(Long workflowID) {
        if (ObjectUtils.isEmpty(workflowID)) {
            throw new NullPointerException("发起人员的可发起的工作流标识为空");
        }
    }

    private void postNameNotNull(String postName) {
        if (StringUtils.isEmpty(postName)) {
            throw new NullPointerException("发起人员的发起岗位名称为空");
        }
    }

    private void postCodeNotNull(String postCode) {
        if (StringUtils.isEmpty(postCode)) {
            throw new NullPointerException("发起人员的发起岗位编码为空");
        }
    }

    private void psnNameNotNull(String psnName) {
        if (StringUtils.isEmpty(psnName)) {
            throw new NullPointerException("发起人员的发起人员名称为空");
        }
    }

    private void psnIDNotNull(String psnID) {
        if (StringUtils.isEmpty(psnID)) {
            throw new NullPointerException("发起人员的发起人员标识为空");
        }
    }

    private void typeNotNull(Type type) {
        if (ObjectUtils.isEmpty(type)) {
            throw new NullPointerException("发起人员的发起人员类型为空");
        }
    }

}
